﻿using DataTables.Mvc;
using Inspinia_MVC5.Code;
using Inspinia_MVC5.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Inspinia_MVC5.Controllers
{
    [AllowCors]
    [SessionTimeOutFilter]
    public class DataController : Controller
    {
        // GET: AllData
        public JsonResult AllData(string wellid, int page = 1, int size = 16, string sortkey = null, string search = null)
        {
            string wellstr = Session["wellstr"].ToString();
            if (wellstr == "0" || wellstr.Contains("-" + wellid + "-"))
            {
                List<DataModel> DataModel = new List<DataModel>();
                DataModel = ResultHelper.getDataByWellid(wellid);
                if (DataModel.Count() == 0)
                    return Json(new { status = "3" }, JsonRequestBehavior.AllowGet);
                if (!string.IsNullOrEmpty(search))
                {
                    DataModel = DataModel.Where(u => u.Identifier.Contains(search) || u.Parameter.Contains(search) || u.Style.Contains(search)).ToList();
                }
                DataModel = sortDataModel(sortkey, DataModel);
                return Json(new { status = "0", data = DataModel.Skip((page - 1) * size).Take(size) }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { status = "2" }, JsonRequestBehavior.AllowGet);
            }
        }
        //排序DataModel
        public List<DataModel> sortDataModel(string sortkey, List<DataModel> DataModel)
        {
            //switch (sortkey)
            //{
            //    case "starttime_desc":
            //        DataModel = DataModel.OrderByDescending(w => w.ActionTime).ToList();
            //        break;
            //    case "starttime_asc":
            //        DataModel = DataModel.OrderBy(w => w.ActionTime).ToList();
            //        break;
            //    case "endtime_desc":
            //        DataModel = DataModel.OrderByDescending(w => w.ErrorEndTime).ToList();
            //        break;
            //    case "endtime_asc":
            //        DataModel = DataModel.OrderBy(w => w.ErrorEndTime).ToList();
            //        break;
            //    case "info_desc":
            //        DataModel = DataModel.OrderByDescending(w => w.IDCode).ToList();
            //        break;
            //    case "info_asc":
            //        DataModel = DataModel.OrderBy(w => w.IDCode).ToList();
            //        break;
            //    case "status_desc":
            //        DataModel = DataModel.OrderByDescending(w => w.isCheck).ToList();
            //        break;
            //    case "status_asc":
            //        DataModel = DataModel.OrderBy(w => w.isCheck).ToList();
            //        break;
            //    case "operator_desc":
            //        DataModel = DataModel.OrderByDescending(w => w.OperatorID).ToList();
            //        break;
            //    case "operator_asc":
            //        DataModel = DataModel.OrderBy(w => w.OperatorID).ToList();
            //        break;
            //    default:
            //        break;
            //}
            return DataModel;
        }
        public List<DataModel> sortAllData(string sortkey, List<DataModel> DataModel)
        {
            switch (sortkey)
            {
                case "Datetime desc":
                    DataModel = DataModel.OrderByDescending(w => w.Datetime).ToList();
                    break;
                case "Datetime asc":
                    DataModel = DataModel.OrderBy(w => w.Datetime).ToList();
                    break;
                case "Identifier desc":
                    DataModel = DataModel.OrderByDescending(w => w.Identifier).ToList();
                    break;
                case "Identifier asc":
                    DataModel = DataModel.OrderBy(w => w.Identifier).ToList();
                    break;
                case "Parameter desc":
                    DataModel = DataModel.OrderByDescending(w => w.Parameter).ToList();
                    break;
                case "Parameter asc":
                    DataModel = DataModel.OrderBy(w => w.Parameter).ToList();
                    break;
                case "Value desc":
                    DataModel = DataModel.OrderByDescending(w => w.Value).ToList();
                    break;
                case "Value asc":
                    DataModel = DataModel.OrderBy(w => w.Value).ToList();
                    break;
                case "Unit desc":
                    DataModel = DataModel.OrderByDescending(w => w.Unit).ToList();
                    break;
                case "Unit asc":
                    DataModel = DataModel.OrderBy(w => w.Unit).ToList();
                    break;
                case "Range desc":
                    DataModel = DataModel.OrderByDescending(w => w.Range).ToList();
                    break;
                case "Range asc":
                    DataModel = DataModel.OrderBy(w => w.Range).ToList();
                    break;
                case "Style desc":
                    DataModel = DataModel.OrderByDescending(w => w.Style).ToList();
                    break;
                case "Style asc":
                    DataModel = DataModel.OrderBy(w => w.Style).ToList();
                    break;
                default:
                    break;
            }
            return DataModel;
        }
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult GetAllData([ModelBinder(typeof(DataTablesBinder))] IDataTablesRequest requestModel, string wellid)
        {
            List<DataModel> query = new List<DataModel>();
            string wellstr = Session["wellstr"].ToString();
            if (wellstr == "0" || wellstr.Contains("-" + wellid + "-"))
            {
                query = ResultHelper.getDataByWellid(wellid);
            }
            var totalcount = query.Count();

            #region Filtering
            if (requestModel.Search.Value != string.Empty)
            {
                var value = requestModel.Search.Value.Trim();
                query = query.Where(p => p.Identifier.Contains(value) ||
                                         p.Parameter.Contains(value) ||
                                         p.Value.Contains(value) ||
                                         p.Unit.Contains(value) ||
                                         p.Range.Contains(value) ||
                                         p.Style.Contains(value)
                                    ).ToList();
            }
            var filteredCount = query.Count();
            #endregion

            #region Sorting
            var sortedColumns = requestModel.Columns.GetSortedColumns();
            var orderByString = string.Empty;
            foreach (var column in sortedColumns)
            {
                orderByString += orderByString != string.Empty ? "," : "";
                orderByString += (column.Data) + (column.SortDirection == Column.OrderDirection.Ascendant ? " asc" : " desc");
            }
            query = sortAllData(orderByString, query);
            #endregion
            //Paging
            query = query.Skip(requestModel.Start).Take(requestModel.Length).ToList();

            var data = query.Select(asset => new
            {
                Datetime = asset.Datetime,
                WellId = asset.WellId,
                Identifier = asset.Identifier,
                Parameter = asset.Parameter,
                Value = asset.Value,
                Unit = asset.Unit,
                Range = asset.Range,
                Style = asset.Style
            }).ToList();

            return Json(new DataTablesResponse(requestModel.Draw, data, filteredCount, totalcount), JsonRequestBehavior.AllowGet);
        }

        public JsonResult Data(string wellid)
        {
            string wellstr = Session["wellstr"].ToString();
            if (wellstr == "0" || wellstr.Contains("-" + wellid + "-"))
            {
                List<DataModel> DataModel = new List<DataModel>();
                DataModel = ResultHelper.getDataByWellid(wellid);
                if (DataModel.Count() == 0)
                    return Json(new { status = "3" }, JsonRequestBehavior.AllowGet);
                return Json(new { status = "0", data = DataModel }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { status = "2" }, JsonRequestBehavior.AllowGet);
            }
        }
    }
}